<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>
<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>

<script>
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    $(function () {
        pageInit();
    });

    function pageInit() {
        $("#Used").selectpicker('val', '{{.m.Used}}');
        $("#FunctionTable1").selectpicker('val', '{{.m.FunctionTable1}}');
        $("#FunctionTable2").selectpicker('val', '{{.m.FunctionTable2}}');
        $("#FunctionTable3").selectpicker('val', '{{.m.FunctionTable3}}');
        $("#DataType").selectpicker('val', '{{.m.DataType}}');
        rms.selectMeterType('{{ urlfor "EquipmentMeterTypeController.DataGridSelect"}}', '{{.m.MeterTypeNO}}');

        initCheckbox();

        //验证
        validateInfo();
    }

    function initCheckbox() {
        $("#Needpt").bootstrapSwitch('state', {{.m.Needpt}});

        $("#Needct").bootstrapSwitch('state', {{.m.Needct }});

        $("#Msbbit").bootstrapSwitch('state', {{.m.Msbbit }});

        $("#BigEndian").bootstrapSwitch('state', {{.m.BigEndian }});
    }

    //表单验证及AJAX提交
    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                AddressSort:{
                    required: true,
                    min: 1
                },
                MeterTypeNO: {
                    required: true
                },
                RomAddress:{
                    required: true
                },
                RomName:{
                    required: true
                },
                SegmentNO:{
                    required: true,
                    min: 1
                },
                Calcfactor:{
                    required: true
                }
            },
            messages: {},
            submitHandler: function (form) {
                var url = '{{ urlfor "EquipmentMeterRomConfigController.Edit"}}';
                var params = $thisForm.serialize();
                $.sdpost(url, params, function (re) {
                    if (re.code === 0) {
                        parent.layer.msg(re.msg);
                        parent.refresh(re.obj);
                        parent.layer.close(index);
                    } else {
                        parent.layer.alert(re.msg, {icon: 2, title: '失败'})
                    }
                });
                return false;
            }
        });
    }

    function FormSubmit() {
        $("#thisForm").submit();
    }
</script>